//
//  WBStatusPictureView.swift
//  0829-Weibo
//
//  Created by 毕仲阳 on 16/9/4.
//  Copyright © 2016年 mycompany. All rights reserved.
//

import UIKit
//设定条目间距
private let itemMargin:CGFloat = 5
//设定条目的宽高
private let itemWH = (WBScreenW - 2*WBStatusCellMargin - 2*itemMargin)/3
class WBStatusPictureView: UICollectionView {

    let WBpictureViewCellid = "WBpictureViewCellid"
    ///当前配图显示的配图数据
    var pic_urls:[WBStatusPictureInfo]?{
        didSet
        {
            label.text = "\(pic_urls?.count ?? 0)"
            //要根据图片的数量，计算当前视图的大小
            self.snp_updateConstraints { (make) in
                make.size.equalTo(self.calSize(picCount: pic_urls?.count ?? 0))
            }
            //
            self.reloadData()
        }
    }
    

    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: layout)
        self.setUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setUI()
    {
        //self.backgroundColor = UIColor.orange
        //添加控件
        addSubview(label)
        label.snp_makeConstraints { (make)->Void in
            make.center.equalTo(self)
        }
        //注册单元格
        self.register(WBStatusPictureCell.classForCoder(), forCellWithReuseIdentifier: WBpictureViewCellid)
        self.dataSource = self
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSize(width: itemWH, height: itemWH)
        layout.minimumLineSpacing = itemMargin
        layout.minimumInteritemSpacing = itemMargin
    }
    
    ///根据图片数量，返回视图的 size
    private func calSize(picCount:Int)->CGSize{
        if picCount == 1
        {
            if let size = pic_urls?.first?.size{
                let scale = UIScreen.main.scale
                let result = CGSize(width: scale*size.width, height: scale*size.height)
                
                let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
                layout.itemSize = result
                return result
            }
        }
//        //设定条目间距
//        let itemMargin:CGFloat = 5
//        //设定条目的宽高
//        let itemWH = (WBScreenW - 2*WBStatusCellMargin - 2*itemMargin)/3
        //设定列数
        var col = picCount > 3 ? 3 : picCount
        if picCount == 4 {
            col = 2
        }
        //设定行数
        let row = (picCount - 1)/3 + 1
        
        //通过行数、列数、条目宽高计算控件的宽高
        let width = CGFloat(col) * itemWH + CGFloat(col - 1)*itemMargin
        let height = CGFloat(row) * itemWH + CGFloat(row - 1)*itemMargin
        
        // 如果是多张图片，要调条目的大小调整回来
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        // 将条目大小调整成正方形
        layout.itemSize = CGSize(width: itemWH, height: itemWH)

        return CGSize(width: width, height: height)
        
    }
    ///懒加载控件
    private var label : UILabel = {
       let label = UILabel(textColor: UIColor.black, fontSize: 30)
        return label
    }()
}

//MARK: - 
extension WBStatusPictureView:UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pic_urls?.count ?? 0
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = self.dequeueReusableCell(withReuseIdentifier: WBpictureViewCellid, for: indexPath) as! WBStatusPictureCell
//        let color = RandomColor
//        cell.backgroundColor = color
        
        //let model = pic_urls?[indexPath.item]
        let model = pic_urls![indexPath.item]
        cell.pictureInfo = model
        return cell
        
    }
}
class WBStatusPictureCell: UICollectionViewCell {
    var pictureInfo : WBStatusPictureInfo?{
        didSet{
            self.imageView.sd_setImage(with: URL(string:pictureInfo?.thumbnail_pic ?? ""), placeholderImage: #imageLiteral(resourceName: "timeline_image_placeholder"))
        }
        
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUI(){
        self.contentView.addSubview(imageView)
        imageView.snp_makeConstraints { (make)->Void in
            make.edges.equalTo(self.contentView)
        }
    }
    //懒加载控件
    lazy var imageView : UIImageView = {
        let view = UIImageView()
        view.contentMode = UIViewContentMode.scaleAspectFill
        view.clipsToBounds = true
        return view
    }()
}
